#!/bin/bash
#
# Target dist: Ubuntu xenial
DIST=xenial

rm -rf ${DIST}
cp -r debian ${DIST}

# mi_xmlrpc module
sed -i 's/libxmlrpc-c3-dev/libxmlrpc-core-c3-dev/' ${DIST}/control

# old mysql
sed -i -e 's/default-mysql-client/mysql-client/' \
  -e 's/default-libmysqlclient-dev/libmysqlclient-dev/' ${DIST}/control
echo "kamailio source: build-depends-on-obsolete-package build-depends: libmysqlclient-dev => default-libmysqlclient-dev" >> \
	${DIST}/source.lintian-overrides
echo "kamailio-mysql-modules: depends-on-obsolete-package depends: mysql-client => default-mysql-client" >> \
	${DIST}/kamailio-mysql-modules.lintian-overrides

# geocoding not in libphonenumber
sed -i 's/libphonenumber-dev/libgeocoding-dev \(>= 7\),\n libphonenumber-dev/' \
	${DIST}/control

# libbson not in libmongoc
sed -i 's/libmongoc-dev/libbson-dev,\n libmongoc-dev/' \
	${DIST}/control

# No secsipid module
sed -i -e '/libsecsipid-dev/d' -e '/^Package: kamailio-secsipid-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += secsipid/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += secsipid' ${DIST}/rules

# libwebsockets >= 3.1
sed -i -e '/libwebsockets-dev/d' -e '/^Package: kamailio-lwsc-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += lwsc/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += lwsc' ${DIST}/rules

# no libnats-dev
sed -i -e '/^ libnats-dev,/d' \
  -e '/^Package: kamailio-nats-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += nats/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += nats' ${DIST}/rules

# libmicrohttpd too old
sed -i -e '/^ libmicrohttpd-dev,/d' \
  -e '/^Package: kamailio-microhttpd-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += microhttpd/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += microhttpd' ${DIST}/rules

# libssl >= v1.1.1
sed -i -e '/^Package: kamailio-ims-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += ims/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += ims' ${DIST}/rules

# libwolfssl-dev < 5.2
sed -i -e '/^ libwolfssl-dev,/d' \
  -e '/^Package: kamailio-wolftls-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += tls_wolfssl/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += tls_wolfssl' ${DIST}/rules

# tlsa build fails
sed -i -e 's/KTLS_INCLUDE_TLSA=yes/KTLS_INCLUDE_TLSA=no/' ${DIST}/rules

wrap-and-sort -sat -d ${DIST}

# clean backports scripts
rm -rf ${DIST}/backports
exit 0
